object ScoreManagementSystem extends App {
  val subjects = List("数学", "语文", "英语", "物理", "化学")
  case class Student(id: String, name: String, className: String, scores: List[Double])
  val allStudents = (1 to 4).flatMap { classNum =>
    (1 to 30).map { stuNum =>
      val className = s"班级$classNum"
      val id = s"${classNum}_${stuNum}"
      val name = s"学生${classNum}_${stuNum}"
      val scores = subjects.map(_ => 60 + scala.util.Random.nextDouble() * 40)
      Student(id, name, className, scores)
    }
  }.toList
  println("=== 1. 学生总分与平均分 ===")
  for (stu <- allStudents) {
    val total = stu.scores.sum
    val avg = total / stu.scores.length
    println(s"学号: ${stu.id}, 姓名: ${stu.name}, 班级: ${stu.className}, 总分: ${total.round}, 平均分: ${avg.round}")
  }
  println("\n=== 2. 班级分数段统计 ===")
  val classScoreRanges = collection.mutable.Map[String, Map[String, Int]]()
  for (className <- allStudents.map(_.className).distinct) {
    val classStudents = allStudents.filter(_.className == className)
    val rangeCounts = collection.mutable.Map(
      "90+" -> 0, "80-89" -> 0, "70-79" -> 0, "60-69" -> 0, "60-" -> 0
    )
    for (stu <- classStudents; score <- stu.scores) {
      score match {
        case s if s >= 90 => rangeCounts("90+") += 1
        case s if s >= 80 => rangeCounts("80-89") += 1
        case s if s >= 70 => rangeCounts("70-79") += 1
        case s if s >= 60 => rangeCounts("60-69") += 1
        case _ => rangeCounts("60-") += 1
      }
    }
    classScoreRanges(className) = rangeCounts.toMap
  }
  classScoreRanges.foreach { case (cls, ranges) =>
    println(s"班级: $cls")
    ranges.foreach { case (range, count) =>
      println(s"  分数段 $range: $count 人")
    }
  }
  println("\n=== 3. 优秀学生筛选 ===")
  val excellentStudents = for {
    stu <- allStudents
    if stu.scores.forall(_ >= 60)
    avg = stu.scores.sum / stu.scores.length
    if avg >= 85
  } yield stu
  excellentStudents.foreach { stu =>
    val avg = stu.scores.sum / stu.scores.length
    println(s"学号: ${stu.id}, 姓名: ${stu.name}, 班级: ${stu.className}, 平均分: ${avg.round}")
  }
  println("\n=== 4. 班级各科平均分排名 ===")
  for (className <- allStudents.map(_.className).distinct) {
    val classStudents = allStudents.filter(_.className == className)
    val subjectAverages = subjects.map { subj =>
      val idx = subjects.indexOf(subj)
      val avg = classStudents.map(_.scores(idx)).sum / classStudents.length
      (subj, avg)
    }.sortBy(-_._2)
    println(s"班级: $className")
    subjectAverages.zipWithIndex.foreach { case ((subj, avg), rank) =>
      println(s"  第${rank + 1}名: $subj, 平均分: ${avg.round}")
    }
  }
  println("\n=== 5. 年级总分排名前10 ===")
  val top10Students = allStudents.map { stu =>
    val total = stu.scores.sum
    (stu, total)
  }.sortBy(-_._2).take(10)
  top10Students.zipWithIndex.foreach { case ((stu, total), rank) =>
    println(s"第${rank + 1}名: 姓名: ${stu.name}, 班级: ${stu.className}, 总分: ${total.round}")
  }
}